package cn.sangyy.car.controller;

import cn.sangyy.car.query.CarResourcesQuery;
import cn.sangyy.car.service.ICarResourcesService;
import cn.sangyy.car.domain.CarResources;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carResources")
public class CarResourcesController {
    @Autowired
    public ICarResourcesService carResourcesService;

    /**
     * 保存和修改公用的
     * @param carResources  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CarResources carResources){
        try {
            if( carResources.getId()!=null)
                carResourcesService.updateById(carResources);
            else
                carResourcesService.insert(carResources);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            carResourcesService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 通过id获取对象信息
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            CarResources carResources = carResourcesService.selectById(id);
            return AjaxResult.me().setResultObj(carResources);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有
    * @return
    */
    @GetMapping("/list")
    public AjaxResult list(){
        try {
            List<CarResources> list = carResourcesService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/listPage")
    public AjaxResult listPageByQuery(@RequestBody CarResourcesQuery query) {
        Page<CarResources> page = new Page<CarResources>(query.getPage(),query.getRows());
        page = carResourcesService.selectPage(page);
        PageList<CarResources> pageList = new PageList<CarResources>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
    * @author Sangyy
    * 批量新增
    */
    @PostMapping("/batch")
    public AjaxResult batch(@RequestBody List<CarResources> carResourcesList){
        carResourcesService.insertBatchCarRe(carResourcesList);
        return AjaxResult.me();
    }
    /**
    * @author Sangyy
    * 根据car_id查询资源
    */
    @GetMapping("/getResources/{carId}")
    public AjaxResult getResourcesByCarId(@PathVariable Long carId){
        List<CarResources> resources = carResourcesService.selectList(new EntityWrapper<CarResources>()
                .eq("car_id", carId));
        return AjaxResult.me().setResultObj(resources);
    }
    /**
    * @author Sangyy
    * 根据图片路径删除图片
    */
    @DeleteMapping("/imgUrl")
    public AjaxResult deleteImage(@RequestParam("imgUrl") String imgUrl){
        boolean delete = carResourcesService.delete(new EntityWrapper<CarResources>().eq("resource_url", imgUrl));
        if(delete){
            return AjaxResult.me();
        }else {
            return AjaxResult.me().setCode(201).setMessage("删除文件失败！");
        }
    }

}
